package session;

import cart.ShoppingCart;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author osman
 */
@ManagedBean(name = "checkoutManager")
@SessionScoped
public class CheckoutManager {

	@PersistenceContext(unitName = "jsf_crudPU")
	private EntityManager em;
	@EJB
	private OrderManager orderManager;
	private UIInput name;
	private UIInput email;
	private UIInput phone;
	private UIInput address;
//	private UIInput cityRegion;
	private UIInput creditcard;
	private SessionContext context;

	private boolean orderFailure = false;

	public void validateEmail(FacesContext context,
					UIComponent toValidate, Object value) {
		String emailTyped = (String) value;

		if (!emailTyped.contains("@")) {
			String message = "Email address should contain a '@' symbol";
			((UIInput) toValidate).setValid(false);
			context.addMessage(toValidate.getClientId(context),
							new FacesMessage(message));
		}
	}

	public String purchase() {
		HttpServletRequest request =
						(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		HttpSession session = request.getSession();

		ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
		int orderId =
						orderManager.placeOrder(
						(String) name.getValue(),
						(String) email.getValue(),
						(String) phone.getValue(),
						(String) address.getValue(),
						"1",
						(String) creditcard.getValue(),
						cart);

		// if order processed successfully send user to confirmation page
		if (orderId != 0) {

			// dissociate shopping cart from session
			cart = null;

			// end session
			session.invalidate();

			// get order details
			Map orderMap = orderManager.getOrderDetails(orderId);

			// place order details in request scope
			request.setAttribute("customer", orderMap.get("customer"));
			request.setAttribute("products", orderMap.get("products"));
			request.setAttribute("orderRecord", orderMap.get("orderRecord"));
			request.setAttribute("orderedProducts", orderMap.get("orderedProducts"));

			return "confirmation";

			// otherwise, send back to checkout page and display error
		} else {
			orderFailure = true;
			return "checkout";
		}
	}

	public UIInput getAddress() {
		return address;
	}

	public void setAddress(UIInput address) {
		this.address = address;
	}

	public UIInput getCreditcard() {
		return creditcard;
	}

	public void setCreditcard(UIInput creditcard) {
		this.creditcard = creditcard;
	}

	public UIInput getEmail() {
		return email;
	}

	public void setEmail(UIInput email) {
		this.email = email;
	}

	public UIInput getName() {
		return name;
	}

	public void setName(UIInput name) {
		this.name = name;
	}

	public UIInput getPhone() {
		return phone;
	}

	public void setPhone(UIInput phone) {
		this.phone = phone;
	}

	public boolean isOrderFailure() {
		return orderFailure;
	}

	public void setOrderFailure(boolean orderFailure) {
		this.orderFailure = orderFailure;
	}
}
